package com.hua.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hua.common.Response;
import com.hua.common.ResponseCode;
import com.hua.domain.MixIngredients;
import com.hua.exception.BusinessException;
import com.hua.mapper.MixIngredientsMapper;
import com.hua.service.MixIngredientsService;
import com.hua.util.RedisCache;
import com.hua.util.ResultUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Distance
 * @description 针对表【mix_ingredients(添加料的表  也是用户可以自行添加的料)】的数据库操作Service实现
 * @createDate 2022-10-19 19:06:35
 */
@Service
public class MixIngredientsServiceImpl extends ServiceImpl<MixIngredientsMapper, MixIngredients> implements MixIngredientsService {

    @Resource
    private MixIngredientsMapper mixIngredientsMapper;

    @Resource
    private RedisCache redisCache;

    @Override
    public Response<List<MixIngredients>> getAll() {

        List<MixIngredients> mixIngredients = redisCache.getCacheList("mixIngredients");

        if(mixIngredients.size()!=0){
            return  ResultUtils.success(ResponseCode.SUCCESS, mixIngredients, null, "返回所有底料");
        }
        mixIngredients = mixIngredientsMapper.selectList(null);

        if(mixIngredients.size() == 0) {
            throw new BusinessException(ResponseCode.SYSTEM_ERROR,"数据为空~");
        }
        redisCache.setCacheList("mixIngredients",mixIngredients);

        return  ResultUtils.success(ResponseCode.SUCCESS, mixIngredients, null, "返回所有底料");

    }

    @Override
    public Response<String> insertMixIngredient(MixIngredients mixIngredients) {
        if (mixIngredientsMapper.insert(mixIngredients) > 0) {
            redisCache.deleteObject("mixIngredients");
            return ResultUtils.success(ResponseCode.SUCCESS, "更新成功");
        }
        throw new BusinessException(ResponseCode.UPDATE_ERROR, "更新失败");

    }

    @Override
    public Response<String> deleteById(Integer id) {
        if(mixIngredientsMapper.deleteById(id) > 0) {
            redisCache.deleteObject("mixIngredients");
            return ResultUtils.success(ResponseCode.SUCCESS, "删除成功");
        }
        throw new BusinessException(ResponseCode.SYSTEM_ERROR, "删除失败");
    }

    @Override
    public Response<String> updateMixIngredient(MixIngredients mixIngredients) {
        if(mixIngredientsMapper.updateById(mixIngredients) > 0) {
            redisCache.deleteObject("mixIngredients");
            return ResultUtils.success(ResponseCode.SUCCESS, "更新成功");
        }
        throw new BusinessException(ResponseCode.UPDATE_ERROR, "更新失败");
    }
}




